<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta content="width=device-width, initial-scale=1, maximum-scale=1" name="viewport">
    <title>权限列表</title>
    <link href="../layui-v2.5.6/css/layui.css" rel="stylesheet">
</head>
<body>
<div class="dBody">
    <table class="layui-hidden" id="treeTable" lay-filter="treeTable"></table>
</div>
</body>
<script src="../jquery/jquery-3.4.1.js"></script>
<script src="../layui-v2.5.6/layui.all.js"></script>
<script type="text/javascript">
    var pageCurr, treeTable;
    $(function () {
        layui.config({
            base: '../layui-v2.5.6/lay/extendmodules/'
        }).extend({
            treeGrid: 'treeGrid'
        }).use(['treeGrid'], function () {
            var treeGrid = layui.treeGrid;
            treeTable = treeGrid.render({
                id: 'treeTable',
                elem: '#treeTable',
                idField: 'id',
                url: 'query',
                treeId: 'id',
                treeUpId: 'pid',
                treeShowName: 'name',
                cols: [[{
                    field: 'id',
                    title: '权限id'
                }, {
                    field: 'name',
                    title: '权限名称'
                }, {
                    field: 'pid',
                    title: '父级权限id'
                }, {
                    field: 'zindex',
                    title: '优先级'
                }, {
                    field: 'code',
                    title: '权限编号'
                }, {
                    field: 'page',
                    title: '权限url'
                }, {
                    field: 'insertTime',
                    title: '添加时间'
                }, {
                    field: 'updateTime',
                    title: '更新时间'
                }, {
                    title: '操作',
                    align: 'center',
                    templet: function (d) {
                        var html = '';
                        var editBtn = '<a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">编辑</a>';
                        var addBtn = '<a class="layui-btn layui-btn-xs" lay-event="add">添加子解点</a>';
                        var delBtn = '<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>';
                        return editBtn + addBtn + delBtn;
                    }
                }]],
                page: false
            });
            treeGrid.on('tool(treeTable)', function (obj) {
                if (obj.event === 'edit') {
                    edit(obj.data.id);
                } else if (obj.event === "add") {
                    add(obj.data.id);
                } else if (obj.event === "del") {
                    del(obj.data.id);
                }
            });

        })
    })

    function add(pid) {
        layer.open({
            type: 2,
            title: '新增权限',
            content: 'edit?pid=' + pid,
            area: ['50%', '90%'],
            end: function (index, layero) {
                location.reload();
                return false;
            }
        });
    }

    function edit(id) {
        layer.open({
            type: 2,
            title: "编辑权限",
            content: 'edit?id=' + id,
            area: ['50%', '90%'],
            end: function (index, layero) {
                location.reload();
                return false;
            }
        });
    }

    function del(id) {
        $.ajax({
            type: "POST",
            url: 'del?id=' + id,
            success: function (data) {
                if (data.status == 0) {
                    layer.alert(data.message, function () {
                        layer.closeAll();
                        location.reload();
                    });
                } else {
                    layer.alert(data.message);
                }
            },
            error: function (data) {
                layer.alert("操作请求错误，请您稍后再试");
            }
        });
    }
</script>
</html>